package com.braveo.dict;

import java.util.logging.Level;
import java.util.logging.Logger;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class TtsManager {
	private TextToSpeech myTts;
	private Context parent;
	
	private static TtsManager instance;	
	
	private TtsManager(Context parent){
		this.parent = parent;
		try {
			myTts = new TextToSpeech(parent, ttsInitListener);
		}catch(Exception e){
			Logger.getLogger("TtsManager").log(Level.WARNING, "Set up tts error", e);
		}
	}
	
	public static TtsManager getInstance(Context parent) {
		if(instance == null || parent != instance.parent){ //if the parent change, it means the system restarts, there should be some method to check the availability of an activity
			instance = new TtsManager(parent);
		}
		return instance;
	}
	
	public TextToSpeech getTts(){
		try {
			if(myTts == null)
				myTts = new TextToSpeech(parent, ttsInitListener);
		}catch(Exception e){
			Logger.getLogger("TtsManager").log(Level.WARNING, "Set up tts error", e);
		}		
		return myTts;
	}
	
	public static TextToSpeech getTts(Context parent){
		return getInstance(parent).getTts();
	}
	
	private OnInitListener ttsInitListener = new OnInitListener() {
		
		public void onInit(int status) {
			// TODO Auto-generated method stub
			
		}
	};
	
}
